/*
 * @(#)F5CommonCollector
 *
 * Copyright 2001-2005 友联创新科技
 *
 */
package com.sinodata.bsm.cicp.collector.net.f5;

import java.util.List;

import org.apache.commons.lang.math.NumberUtils;
import org.apache.log4j.Logger;

import com.sinodata.bsm.cicp.collector.Collector;
import com.sinodata.bsm.cicp.exception.CollectorException;
import com.sinodata.bsm.common.vo.ResPropertyValue;
import com.sinodata.bsm.common.vo.ResPropertyValue.PropValue;
import com.union.net.snmp.SnmpGet;

/**
 * F5CommonCollector.java
 * <p>
 * F5基本指标信息采集，CPU、内存
 *
 * @author 李祥军 lixj@uni-mon.com
 * @version 1.0
 * @Date 2010-6-28
 */
public class F5CommonCollector extends Collector {
    static String[] ParamMeta = { "共同体" };
    static String[] PropMeta = { "CPU使用率", "内存使用率", "内存总量", "已用内存", "空闲内存" };
    static Logger logger = Logger.getLogger(F5CommonCollector.class);

    public List<ResPropertyValue> collect(String ip) throws CollectorException {
        SnmpGet get = null;
        try {
            get = new SnmpGet(ip);
            get.setCommunity(getParamMap().get(ParamMeta[0]));
            String sysStatTmTotalCycles = get.getSingle("1.3.6.1.4.1.3375.2.1.1.2.1.41");
            String sysStatTmIdleCycles = get.getSingle("1.3.6.1.4.1.3375.2.1.1.2.1.42");
            String totalmem = get.getSingle("1.3.6.1.4.1.3375.2.1.1.2.1.44");
            String usedmem = get.getSingle("1.3.6.1.4.1.3375.2.1.1.2.1.45");
            PropValue[] propValues = new PropValue[5];

            if (NumberUtils.isNumber(sysStatTmTotalCycles) && NumberUtils.isNumber(sysStatTmIdleCycles)) {
                long _sysStatTmTotalCycles = NumberUtils.toLong(sysStatTmTotalCycles);
                long _sysStatTmIdleCycles = NumberUtils.toLong(sysStatTmIdleCycles);
                propValues[0] = ResPropertyValue.createPropValue(((_sysStatTmTotalCycles - _sysStatTmIdleCycles) * 100 / _sysStatTmTotalCycles) + "", PropMeta[0]);
            }
            if (NumberUtils.isNumber(totalmem) && NumberUtils.isNumber(usedmem)) {
                long _totalmem = NumberUtils.toLong(totalmem) / (1024 * 1024);
                long _usedmem = NumberUtils.toLong(usedmem) / (1024 * 1024);
                propValues[2] = ResPropertyValue.createPropValue(_totalmem + "", PropMeta[2]);
                propValues[3] = ResPropertyValue.createPropValue(_usedmem + "", PropMeta[3]);
                propValues[4] = ResPropertyValue.createPropValue((_totalmem - _usedmem) + "", PropMeta[4]);
                propValues[1] = ResPropertyValue.createPropValue(Math.round((double) _usedmem / (double) _totalmem * 100) + "", PropMeta[1]);
            }
            ResPropertyValue propertyValueX = new ResPropertyValue();
            propertyValueX.setValues(propValues);
            return java.util.Arrays.asList(propertyValueX);
        } catch (Exception e) {
            logger.error(e);
            throw new CollectorException(e);
        } finally {
            if (get != null)
                get.close();
        }
    }

    public String[] getParamMeta() {
        return ParamMeta;
    }

    public String[] getPropMeta() {
        return PropMeta;
    }

    public static void main(String[] args) {
        System.out.println(Double.valueOf("68916424628146239"));
    }
}
